/*
 * Extra debug tools for FMSEclipse
 * This script exposes functionality which enables code injection and should
 * only be used for debugging. _rdb_execute_statement tries to make sure only
 * a debug connection can call it but one can never be certain
 */
application.allowDebug = true;
RobustDebug = new Object();

services.onDebugConnect = function(client)
{
	client._rdb_execute_statement = RobustDebug._rdb_execute_statement;
	this.acceptConnection(client);
}

RobustDebug._rdb_execute_statement = function(code)
{
	/*
	 * Will only let a debug connection call this method
	 */
	if(this.uri.indexOf("_fcs_debugreq_") != -1)
	{
		//Local Host
		if(this.referrer.slice(0, 4) == "file")
		{
			var nf = new File("_rdb_live_execute.asc");
			//
			if(nf)
			{
				nf.open("text", "create");
				nf.close();
				//
				if(nf.open("text", "write"))
				{
					nf.write(code);
					nf.flush();
					nf.close();
					//
					trace("Live Execution Start");
					load("_rdb_live_" + "execute.asc");
					trace("Live Execution End");
					//
					nf.remove();
					//
				}
			}
		}
	}
}
/*
 * Override the trace function to dump complex objects instead of doing a
 * toString "[object Object]"
 */

RobustDebug._trace = trace; 
//
RobustDebug._dump = function(val)
{
	if (typeof (val) == "object")
	{
		RobustDebug._dumpComplex(val);
	}
	else
	{
		RobustDebug._trace(val);
	}
}
RobustDebug._dumpComplex = function(obj)
{
	var hasRef = new Object();
	var path = "obj.";
	RobustDebug._trace(":@zf_dump:" + RobustDebug.express_object_string(obj, 0, hasRef, path));
}
RobustDebug.typeRef = {robject:0, rstring:1, rnumber:2, rboolean:3, rfunction:4};
RobustDebug.express_object_string = function(obj, level, hasRef, path)
{
	//To stop infinate loops
	if(!hasRef[path])
	{
		hasRef[path] = true;
	}else{
		return false;
	}
	//
	var rStr = "";
	var end = "?";
	for(var i in obj)
	{
		var o = obj[i];
		if(o === null)
		{
			o = "%null%";
		}
		if(o != null)
		{
			var t = this.typeRef["r"+typeof(o)];
			//
			var prefix;
			var suffix;
			//
			if(t == 0)
			{
				prefix = end+level+":0,"+i+",";
				suffix = "0:"+level+end+",";
				var nobj = this.express_object_string(o, level+1, hasRef, path + i + ".");
				if(nobj != false)
				{
					rStr += prefix+nobj+suffix;
				}
			}else if(t == 4)
			{
				prefix = end+level+":"+t+","+i+","+"function";
				suffix = ","+t+":"+level+end+",";
				rStr += prefix+""+suffix;
			}else if(t == 2)
			{
				prefix = end+level+":"+t+","+i+",";
				suffix = ","+t+":"+level+end+",";
				rStr += prefix+"%"+o.toString()+"%"+suffix;
			}else{
				
				if(o != "%null%")
				{
					prefix = end+level+":"+t+","+i+",";
					suffix = ","+t+":"+level+end+",";
					rStr += prefix+o.toString()+suffix;
					
				}else{
					
					prefix = end+level+":"+5+","+i+",";
					suffix = ","+5+":"+level+end+",";
					rStr += prefix+"null"+suffix;
					
				}
				
			}
		}
	}
	return rStr;
}
//
this.__defineGetter__("trace", function(){ return RobustDebug._dump;});